package ylsb.service;


import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ylsb.dao.ShijiMapper;
import ylsb.model.*;
import ylsb.support.JsonResult;
import ylsb.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShijiService {

    @Resource
    private ShijiMapper ddMapper;

    private static int startindex = 0;

    public List<Map<String, Object>> findPinzhongAll(String ppxiangmuId){
        return ddMapper.findPinzhongAll(ppxiangmuId);
    }
    public List<Map<String, Object>> findGuochanPinzhong(String ppxiangmuId){
        return ddMapper.findGuochanPinzhong(ppxiangmuId);
    }
    public List<Map<String, Object>> findFengbi(String ppxiangmuId){
        return ddMapper.findFengbi(ppxiangmuId);
    }
    public List<Map<String, Object>> findFengbiCount(String ppxiangmuId,String ppPinzhong){
        return ddMapper.findFengbiCount(ppxiangmuId,ppPinzhong);
    }
    public List<Map<String, Object>> findGuochanBypinzhong(String ppxiangmuId,String ppPinzhong){
        return ddMapper.findGuochanBypinzhong(ppxiangmuId,ppPinzhong);
    }
    public List<Map<String, Object>> findResultBypinzhong(String ppxiangmuId, String ppuserId,String ppPinzhong){
        return ddMapper.findResultBypinzhong(ppxiangmuId,ppuserId,ppPinzhong);
    }

    public List<Map<String, Object>> findResultGuochanBypinzhong(String ppxiangmuId, String ppuserId,String ppPinzhong){
        return ddMapper.findResultGuochanBypinzhong(ppxiangmuId,ppuserId,ppPinzhong);
    }

    public List<Map<String, Object>> findAllByXiangmuIdAndTiaojian(String xiangmuId,String mmleibie,String mmpinpaiqingkuang){
        return ddMapper.findAllByXiangmuIdAndTiaojian(xiangmuId,mmleibie,mmpinpaiqingkuang);
    }

    public List<Map<String, Object>> findXinghaoByPinpai(String ppxiangmuId,String ppPinzhong,String ppPinpai){
        return ddMapper.findXinghaoByPinpai(ppxiangmuId,ppPinzhong,ppPinpai);
    }

    public Integer getPinguiCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPinguiCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
    public Integer getPinzhongCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPinzhongCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
    public Integer getPaichuPinguiCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPaichuPinguiCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Shiji findOne(String id){
        return ddMapper.selectByPrimaryKey(id);
    }

    public List<Map<String,Object>> getCountByPinzhongAndPinpai(int mmPinzhong,String mmPinpai,String mmxiangmuId){
        return ddMapper.getCountByPinzhongAndPinpai(mmPinzhong,mmPinpai,mmxiangmuId);
    }

    public JsonResult<?> findshijilist(String ppxiangmuId, int ppPageIndex, int ppPageSize) {

        List<Shijipinzhong> pinzhongs = ddMapper.selectAllByXiangmuid(ppxiangmuId, (ppPageIndex - 1) * ppPageSize, ppPageSize);
        List<Map<String,Object>> shijiList = new ArrayList<>();
        for (Shijipinzhong pinzhong : pinzhongs) {
            Map<String, Object> pinzhongMap = new HashMap<>();
            List<Map<String,Object>> returnPinpaiList = new ArrayList<>();
            int pinzhonghebinghangshu = 0;
            for (Shijipinpai pinpai : pinzhong.getPinpais()) {
                Map<String, Object> pinpaiMap = new HashMap<>();
                pinpaiMap.put("pinpaixuhao", pinpai.getPinpai());
                int size = pinpai.getXinghaos().size();
                pinpaiMap.put("pinpaihebinghang", size == 0 ? 1 : size);
                pinpaiMap.put("xinghaolist", pinpai.getXinghaos());
                returnPinpaiList.add(pinpaiMap);
                pinzhonghebinghangshu += size;
            }
            pinzhongMap.put("pinzhongming", pinzhong.getPinzhongming());
            pinzhongMap.put("pinzhongxuhao", pinzhong.getPinzhongxuhao());
            pinzhongMap.put("pinpailist", returnPinpaiList);
            pinzhongMap.put("pinzhonghebinghang", pinzhonghebinghangshu == 0 ? pinzhong.getPinpais().size() : pinzhonghebinghangshu);
            shijiList.add(pinzhongMap);
        }
        int count = getShijilist(ppxiangmuId,"","", ppPageIndex, ppPageSize);
        PageInfo pageInfo = new PageInfo(ppPageIndex, ppPageSize, count);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ShijiList", shijiList);
        returnMap.put("PageInfo", pageInfo);

        return JsonResult.getSuccessResult(returnMap);
    }

    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,
                                         @RequestParam("tiaojian") String pptiaojian,
                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize) {
        String mmleibie="";
        String mmpinpaiqingkuang="";
        if(pptiaojian.equals("封闭试剂")){
             mmleibie=pptiaojian;
        }else if(pptiaojian.equals("一个品牌")){
            mmpinpaiqingkuang=pptiaojian;
        }else if(pptiaojian.equals("两个品牌")){
            mmpinpaiqingkuang=pptiaojian;
        }else if(pptiaojian.equals("专机专用")){
            mmpinpaiqingkuang=pptiaojian;
        }


        List<Shijipinzhong> pinzhongAll = ddMapper.selectBiaojueByXiangmuid(ppxiangmuId, ppuserId,mmleibie,mmpinpaiqingkuang, 0, 9999);
        List<Map<String,Object>> shijiList = new ArrayList<>();

        int startindex = 0;
        if (ppPageIndex != 1){
            for (int i = 0; i < (ppPageIndex - 1) * ppPageSize; i++) {
                if (i >= pinzhongAll.size())
                    break;
                for (Shijipinpai pinpai : pinzhongAll.get(i).getPinpais()) {
                    startindex += pinpai.getXinghaos().size();
                }
            }
        }
        int pageSize = 0;
        for (int i = (ppPageIndex - 1) * ppPageSize; i < ppPageIndex * ppPageSize; i++) {
            if (i >= pinzhongAll.size())
                break;
            for (Shijipinpai pinpai : pinzhongAll.get(i).getPinpais()) {
                pageSize += pinpai.getXinghaos().size();
            }
        }
        List<Shijipinzhong> pinzhongs = ddMapper.selectBiaojueByXiangmuid(ppxiangmuId, ppuserId,mmleibie,mmpinpaiqingkuang, startindex, pageSize);


        for (Shijipinzhong pinzhong : pinzhongs) {
            Map<String, Object> pinzhongMap = new HashMap<>();
            List<Map<String,Object>> returnPinpaiList = new ArrayList<>();
            int pinzhonghebinghangshu = 0;

            for (Shijipinpai pinpai : pinzhong.getPinpais()) {
                String pinpairesult = "0";
                for (Shijixinghao xinghao : pinpai.getXinghaos())
                    if ("1".equals(xinghao.getResult())) {
                        pinpairesult = "1";
                    }
                Map<String, Object> pinpaiMap = new HashMap<>();
                pinpaiMap.put("pinpaixuhao", pinpai.getPinpai());
                pinpaiMap.put("pinpairesult", pinpairesult);
                int size = pinpai.getXinghaos().size();
                pinpaiMap.put("pinpaihebinghang", size == 0 ? 1 : size);
                pinpaiMap.put("xinghaolist", pinpai.getXinghaos());
                returnPinpaiList.add(pinpaiMap);
                pinzhonghebinghangshu += size;
            }
            pinzhongMap.put("pinzhongming", pinzhong.getPinzhongming());
            pinzhongMap.put("pinzhongxuhao", pinzhong.getPinzhongxuhao());
            pinzhongMap.put("pinpailist", returnPinpaiList);
            pinzhongMap.put("pinzhonghebinghang", pinzhonghebinghangshu == 0 ? pinzhong.getPinpais().size() : pinzhonghebinghangshu);
            shijiList.add(pinzhongMap);
        }
        int count = getShijilist(ppxiangmuId,mmleibie,mmpinpaiqingkuang, ppPageIndex, ppPageSize);
        PageInfo pageInfo = new PageInfo(ppPageIndex, ppPageSize, count);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ShijiList", shijiList);
        returnMap.put("PageInfo", pageInfo);

        return JsonResult.getSuccessResult(returnMap);
    }

    public Integer getShijilist(String ppxiangmuId,String mmleibie,String mmpinpaiqingkuang,int ppPageIndex, int ppPageSize) {
        Object mmCount = ddMapper.getShijilist(ppxiangmuId,mmleibie,mmpinpaiqingkuang,ppPageIndex,ppPageSize);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean add (Shiji ddShiji){
        return ddMapper.insert(ddShiji)==1;
    }

    public boolean isexistByxiangmuId(String xiangmuId){
        ShijiExample mmExample=new ShijiExample();
        ShijiExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.selectByExample(mmExample).size()>0;
    }

    public boolean deleteByxiangmuId(String xiangmuId){
        ShijiExample mmExample=new ShijiExample();
        ShijiExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.deleteByExample(mmExample)>=0;
    }
}
